<template>
  <div>
    <editor-lite ref="editor" :showNews="false" :editorData="subscribeReply"></editor-lite>
    <el-button @click="handleSave" type="primary" :loading="isSave">保存</el-button>
    <el-button @click="handleDelReply(subscribeReply.status)">{{subscribeReply.status==1 ? '停用' : '启用'}}</el-button>
  </div>
</template>
<script>
  import {mapMutations, mapActions, mapState} from 'vuex'
  import editorLite from '@/components/common/content/wechatEditorLite2'
  export default {
    data () {
      return {
        appid: this.$route.params.appid,
        isSave: false
      }
    },
    components: {editorLite},
    mounted () {
      this.getSubscribeReply(this.appid)
    },
    computed: {
      ...mapState({
        subscribeReply: state => state.wechatManage.subscribeReply
      }),
      content () {
        return this.$refs.editor.getContent
      }
    },
    methods: {
      ...mapMutations(['UPDATE_SUBSCRIBE_REPLY']),
      ...mapActions(['getSubscribeReply', 'setSubscribeReply', 'setIfSubReply']),
      // 保存
      async handleSave () {
        if (this.$refs.editor.getContent.msgType === 'text' && !document.getElementById('editDiv').innerHTML) {
          this.$message.warning('请输入回复内容')
          return false
        }
        if (this.$refs.editor.getContent.msgType !== 'text' && !this.$refs.editor.getContent.mediaid) {
          this.$message.warning('请选择回复内容')
          return false
        }
        this.isSave = true
        const res = await this.setSubscribeReply({
          appid: this.appid,
          msgType: this.$refs.editor.getContent.msgType,
          mediaid: this.$refs.editor.getContent.mediaid,
          content: this.convertSpecials(document.getElementById('editDiv').innerHTML)
        })
        this.isSave = false
        if (res.data.code === 1) {
          this.$message.success('保存成功')
        }
      },
      /**
      * 转移链接里面的特殊符号
      **/
      convertSpecials(str){
        var arrEntities={'lt':'<','gt':'>','nbsp':' ','amp':'&','quot':'"'}
        return str.replace(/&(lt|gt|nbsp|amp|quot);/ig,function(all,t){return arrEntities[t];})
      },
      // 删除回复
      handleDelReply (status) {
        this.setIfSubReply({
          appid: this.appid,
          status: status === 1 ? 0 : 1
        })
      }
    }
  }
</script>
